package com.it.you.base.stream;

import com.alibaba.fastjson.JSON;
import com.it.you.pojo.UserPO;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest8 {

    public static void main(String[] args) {
        String[] str = {"java", "you", "hui", "爱", "分", "享"};
        List<String> list = Arrays.asList(str);

        System.out.println("// 最简单的Lambda");
        list.forEach(s -> System.out.print(s + "-"));
        System.out.println("");

        List<UserPO> poList = new ArrayList<>();
        poList.add(new UserPO("zhang三", 12, "01", 100.2, "北京"));
        poList.add(new UserPO("李四", 18, "01", 990.2, "北京"));
        poList.add(new UserPO("王五", 15, "02", 45.8, "深圳"));
        poList.add(new UserPO("八戒", 8, "02", 1000.8, "上海"));
        poList.add(new UserPO("悟空", 9, "02", 100.2, "上海"));
        poList.add(new UserPO("小红", 12, "01", 50.2, "上海"));

        System.out.println("// 过滤年龄小于10的人");
        List<UserPO> collect1 = poList.stream().filter(u -> u.getAge() > 10).collect(Collectors.toList());
        collect1.forEach(s -> System.out.println(JSON.toJSON(s)));

        System.out.println("// 按地址分组");
        Map<String, List<UserPO>> collect2 = poList.stream().collect(Collectors.groupingBy(UserPO::getAddress));
        collect2.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(JSON.toJSON(v));
        });

        System.out.println("// 按地址分组,然后统计薪水情况");
        Map<String, DoubleSummaryStatistics> collect3 = poList.stream().collect(
                Collectors.groupingBy(UserPO::getAddress, Collectors.summarizingDouble(UserPO::getSalary)));
        collect3.forEach((k, s) -> {
            System.out.println("===============" + k + "==========");
            System.out.println("求和:" + s.getSum());
            System.out.println("求平均" + s.getAverage());
            System.out.println("求最大:" + s.getMax());
            System.out.println("求最小:" + s.getMin());
            System.out.println("求总数:" + s.getCount());
        });

        System.out.println("// 求最大薪水");
        Double max = poList.stream().map(UserPO::getSalary).filter(Objects::nonNull).reduce((double) 0, Double::max);
        UserPO userPO = poList.stream().max(Comparator.comparing(UserPO::getSalary)).get();
        System.out.println("max salary: " + max);
        System.out.println("max salary: " + userPO.getSalary());


        System.out.println("// 按年龄从小到大排序");
        List<UserPO> collect4 =
                poList.stream().sorted(Comparator.comparing(UserPO::getAge)).collect(Collectors.toList());
        collect4.forEach(s -> System.out.println(JSON.toJSON(s)));

        System.out.println("// 按年龄从大到小排序，年龄相同在按名字排序");
        List<UserPO> collect5 =
                poList.stream().sorted(Comparator.comparing(UserPO::getAge).thenComparing(UserPO::getUsername,Comparator.nullsFirst(String::compareTo)).reversed())
                        .collect(Collectors.toList());
        collect5.forEach(s -> System.out.println(JSON.toJSON(s)));

        ArrayList<UserPO> userPOS = null;
        List<UserPO> collect = userPOS.stream().sorted(Comparator.comparing(UserPO::getAge).reversed()).collect(Collectors.toList());
        System.out.println(collect);
    }

}
